package com.keqiang.huaweiscan;

import android.os.Parcel;

import com.huawei.hms.ml.scan.HmsScan;

/**
 * 扫码配置项
 *
 * @author Created by wanggaowan on 2021/6/3 14:56
 */
public class ScanConfig implements android.os.Parcelable {
    private String title;
    private String hint;
    private boolean couldScanImg = true;
    private boolean imgScanFailedBack;
    private String imgScanFailedHint;
    private int appBarColor = -1;
    private int scanLineColor = -1;
    private boolean showLightBtn = false;
    private boolean turnOnLight = false;
    private String applyAlbumPermissionHint;
    private String permanentlyDeniedAlbumPermissionHint;
    
    private int format;
    private int[] formats;
    
    private ScanConfig() {
    
    }
    
    public static ScanConfig copy() {
        return new ScanConfig();
    }
    
    public String getTitle() {
        return title;
    }
    
    /**
     * 设置扫码标题,如果为null，则使用默认值{@link R.string#scan_code_title_label}
     */
    public ScanConfig title(String title) {
        this.title = title;
        return this;
    }
    
    public String getHint() {
        return hint;
    }
    
    /**
     * 设置扫码框底部提示文本，如果为null，则使用默认值{@link R.string#scan_code_hint}
     */
    public ScanConfig hint(String hint) {
        this.hint = hint;
        return this;
    }
    
    public boolean isCouldScanImg() {
        return couldScanImg;
    }
    
    /**
     * 设置扫码框是否可从相册选择内容
     */
    public ScanConfig couldScanImg(boolean couldScanImg) {
        this.couldScanImg = couldScanImg;
        return this;
    }
    
    public boolean isImgScanFailedBack() {
        return imgScanFailedBack;
    }
    
    /**
     * 设置扫描图片失败，是否退出扫码界面
     */
    public ScanConfig imgScanFailedBack(boolean imgScanFailedBack) {
        this.imgScanFailedBack = imgScanFailedBack;
        return this;
    }
    
    public String getImgScanFailedHint() {
        return imgScanFailedHint;
    }
    
    /**
     * 如果扫描图片失败且不退出扫码界面，则提示此文本。如果此值为null，则使用默认值{@link R.string#image_scan_error_hint}
     */
    public ScanConfig imgScanFailedHint(String imgScanFailedHint) {
        this.imgScanFailedHint = imgScanFailedHint;
        return this;
    }
    
    public int getAppBarColor() {
        return appBarColor;
    }
    
    /**
     * 导航条颜色
     */
    public ScanConfig appBarColor(int color) {
        this.appBarColor = color;
        return this;
    }
    
    public int getScanLineColor() {
        return scanLineColor;
    }
    
    /**
     * 二维码扫描线颜色
     */
    public ScanConfig scanLineColor(int color) {
        this.scanLineColor = color;
        return this;
    }
    
    public int getFormat() {
        return format;
    }
    
    public int[] getFormats() {
        return formats;
    }
    
    /**
     * 设置扫码支持的格式，具体可查看{@link HmsScan.SCAN_TYPE}
     *
     * @param format  至少指定一个扫码格式
     * @param formats 其它需要支持的扫码格式
     */
    public ScanConfig formats(int format, int... formats) {
        this.format = format;
        this.formats = formats;
        return this;
    }
    
    public boolean isTurnOnLight() {
        return turnOnLight;
    }
    
    /**
     * 设置是否开启闪光灯
     *
     * @param turnOnLight {@code true}:开启闪光灯 {@code false}:关闭闪光灯
     */
    public ScanConfig turnOnLight(boolean turnOnLight) {
        this.turnOnLight = turnOnLight;
        return this;
    }
    
    /**
     * 是否展示闪光灯控制按钮
     */
    public boolean isShowLightButton() {
        return showLightBtn;
    }
    
    /**
     * 设置是否展示闪光灯控制按钮
     *
     * @param showLightBtn {@code true}:展示 {@code false}:不展示
     */
    public ScanConfig showLightButton(boolean showLightBtn) {
        this.showLightBtn = showLightBtn;
        return this;
    }
    
    /**
     * 请求相册权限前的提示说明
     */
    public String getApplyAlbumPermissionHint() {
        return this.applyAlbumPermissionHint;
    }
    
    /**
     * 设置请求相册权限前的提示说明
     *
     * @param applyAlbumPermissionHint 提示文本，如果为null或空串则不展示。当android版本>=33时,
     *                                 申请的权限为{@link android.Manifest.permission#READ_MEDIA_IMAGES}，
     *                                 否则为{@link android.Manifest.permission#READ_EXTERNAL_STORAGE}。
     *                                 可根据不同版本给与不同的权限提示
     */
    public ScanConfig applyAlbumPermissionHint(String applyAlbumPermissionHint) {
        this.applyAlbumPermissionHint = applyAlbumPermissionHint;
        return this;
    }
    
    /**
     * 相册权限被永久拒绝提示
     */
    public String getPermanentlyDeniedAlbumPermissionHint() {
        return this.permanentlyDeniedAlbumPermissionHint;
    }
    
    /**
     * 设置相册权限被永久拒绝提示
     *
     * @param permanentlyDeniedAlbumPermissionHint 提示文本，如果为null或空串则不展示。当android版本>=33时,
     *                                             申请的权限为{@link android.Manifest.permission#READ_MEDIA_IMAGES}，
     *                                             否则为{@link android.Manifest.permission#READ_EXTERNAL_STORAGE}。
     *                                             可根据不同版本给与不同的权限提示
     */
    public ScanConfig permanentlyDeniedAlbumPermissionHint(String permanentlyDeniedAlbumPermissionHint) {
        this.permanentlyDeniedAlbumPermissionHint = permanentlyDeniedAlbumPermissionHint;
        return this;
    }
    
    @Override
    public int describeContents() {return 0;}
    
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.title);
        dest.writeString(this.hint);
        dest.writeByte(this.couldScanImg ? (byte) 1 : (byte) 0);
        dest.writeInt(this.format);
        dest.writeIntArray(this.formats);
        dest.writeByte(this.imgScanFailedBack ? (byte) 1 : (byte) 0);
        dest.writeString(this.imgScanFailedHint);
        dest.writeInt(this.appBarColor);
        dest.writeInt(this.scanLineColor);
        dest.writeByte(this.showLightBtn ? (byte) 1 : (byte) 0);
        dest.writeByte(this.turnOnLight ? (byte) 1 : (byte) 0);
        dest.writeString(this.applyAlbumPermissionHint);
        dest.writeString(this.permanentlyDeniedAlbumPermissionHint);
    }
    
    protected ScanConfig(Parcel in) {
        this.title = in.readString();
        this.hint = in.readString();
        this.couldScanImg = in.readByte() != 0;
        this.format = in.readInt();
        this.formats = in.createIntArray();
        this.imgScanFailedBack = in.readByte() != 0;
        this.imgScanFailedHint = in.readString();
        this.appBarColor = in.readInt();
        this.scanLineColor = in.readInt();
        this.showLightBtn = in.readByte() != 0;
        this.turnOnLight = in.readByte() != 0;
        this.applyAlbumPermissionHint = in.readString();
        this.permanentlyDeniedAlbumPermissionHint = in.readString();
    }
    
    public static final Creator<ScanConfig> CREATOR = new Creator<ScanConfig>() {
        @Override
        public ScanConfig createFromParcel(Parcel source) {return new ScanConfig(source);}
        
        @Override
        public ScanConfig[] newArray(int size) {return new ScanConfig[size];}
    };
}
